<template>
<div class="profile-edit-container">
    <!-- 顶部导航栏 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.go(-1)" right-text="预览" :border="false" class="nav-bar" />

    <!-- 主要内容 -->
    <div class="profile-content">
        <div class="profile-form">
            <!-- 头像 -->
            <div class="form-item" @click="showUploadPopup = true">
                <div class="form-label">头像</div>
                <div class="form-value avatar-value">
                    <div class="avatar-container">
                        <img :src="userInfo.avatar" class="avatar-img">
                    </div>
                    <van-icon name="arrow" class="arrow-icon" />
                </div>
            </div>

            <!-- 名字 -->
            <div class="form-item" @click="showNicknamePopup = true">
                <div class="form-label">名字</div>
                <div class="form-value">
                    <span class="value-text">{{ userInfo.nickname || '纵横' }}</span>
                    <van-icon name="arrow" class="arrow-icon" />
                </div>
            </div>

            <!-- 小红书号 -->
            <div class="form-item">
                <div class="form-label">小红书号</div>
                <div class="form-value">
                    <span class="value-text">{{ userInfo.userId || '4286474693' }}</span>
                    <van-icon name="arrow" class="arrow-icon" />
                </div>
            </div>

            <!-- 背景图 -->
            <div class="form-item" @click="showBackgroundPopup = true">
                <div class="form-label">背景图</div>
                <div class="form-value">
                    <div class="bg-preview">
                        <img :src="userInfo.backgroundImage || 'https://picsum.photos/300/400?random=1'" class="bg-img">
                    </div>
                    <van-icon name="arrow" class="arrow-icon" />
                </div>
            </div>
        </div>

        <div class="profile-form mt">
            <!-- 简介 -->
            <div class="form-item" @click="showBioPopup = true">
                <div class="form-label">简介</div>
                <div class="form-value">
                    <span class="value-text bio-text">{{ userInfo.bio || '介绍一下自己' }}</span>
                    <van-icon name="arrow" class="arrow-icon" />
                </div>
            </div>
        </div>

        <div class="profile-form mt">
            <!-- 性别 -->
            <div class="form-item" @click="showGenderPopup = true">
                <div class="form-label">性别</div>
                <div class="form-value">
                    <span class="value-text">{{ genderText }}</span>
                    <van-icon name="arrow" class="arrow-icon" />
                </div>
            </div>

            <!-- 生日 -->
            <div class="form-item" @click="showBirthdayPopup = true">
                <div class="form-label">生日</div>
                <div class="form-value">
                    <span class="value-text">{{ userInfo.birthday || '选择生日' }}</span>
                    <van-icon name="arrow" class="arrow-icon" />
                </div>
            </div>

            <!-- 地区 -->
            <div class="form-item" @click="showRegionPopup = true">
                <div class="form-label">地区</div>
                <div class="form-value">
                    <span class="value-text">{{ userInfo.region || '选择所在的地区' }}</span>
                    <van-icon name="arrow" class="arrow-icon" />
                </div>
            </div>
            <!-- 职业 -->
            <div class="form-item" @click="showOccupationPopup = true">
                <div class="form-label">职业</div>
                <div class="form-value">
                    <span class="value-text">{{ userInfo.occupation || '选择职业' }}</span>
                    <van-icon name="arrow" class="arrow-icon" />
                </div>
            </div>

            <!-- 学校 -->
            <div class="form-item" @click="showSchoolPopup = true">
                <div class="form-label">学校</div>
                <div class="form-value">
                    <span class="value-text">{{ userInfo.school || '选择学校' }}</span>
                    <van-icon name="arrow" class="arrow-icon" />
                </div>
            </div>
        </div>
    </div>

    <!-- 保存按钮 -->
    <div class="save-button">
        <van-button round block type="primary" color="#fe496d" @click="saveProfile">保存</van-button>
    </div>

    <!-- 头像上传弹窗 -->
    <van-popup v-model="showUploadPopup" position="bottom" round>
        <div class="upload-popup">
            <div class="popup-title">修改头像</div>
            <div class="upload-options">
                <div class="upload-option" @click="uploadAvatar('camera')">
                    <van-icon name="photograph" size="24" />
                    <span>拍照</span>
                </div>
                <div class="upload-option" @click="uploadAvatar('album')">
                    <van-icon name="photo-o" size="24" />
                    <span>从相册选择</span>
                </div>
            </div>
            <div class="cancel-btn" @click="showUploadPopup = false">取消</div>
        </div>
    </van-popup>

    <!-- 昵称编辑弹窗 -->
    <van-popup v-model="showNicknamePopup" position="bottom" round>
        <div class="edit-popup">
            <div class="popup-header">
                <div class="popup-title">修改昵称</div>
                <van-icon name="cross" @click="showNicknamePopup = false" />
            </div>
            <div class="edit-input">
                <van-field v-model="tempNickname" placeholder="请输入昵称" maxlength="20" show-word-limit />
            </div>
            <div class="popup-buttons">
                <van-button round block type="primary" @click="saveNickname">确定</van-button>
            </div>
        </div>
    </van-popup>

    <!-- 性别选择弹窗 -->
    <van-popup v-model="showGenderPopup" position="bottom" round>
        <div class="edit-popup">
            <div class="popup-header">
                <div class="popup-title">选择性别</div>
                <van-icon name="cross" @click="showGenderPopup = false" />
            </div>
            <div class="gender-options">
                <div class="gender-option" :class="{ active: tempGender === 1 }" @click="tempGender = 1">
                    <van-icon name="male" color="#1989fa" size="20" />
                    <span>男</span>
                </div>
                <div class="gender-option" :class="{ active: tempGender === 2 }" @click="tempGender = 2">
                    <van-icon name="female" color="#ff2c7d" size="20" />
                    <span>女</span>
                </div>
                <div class="gender-option" :class="{ active: tempGender === 0 }" @click="tempGender = 0">
                    <van-icon name="question-o" color="#969799" size="20" />
                    <span>保密</span>
                </div>
            </div>
            <div class="popup-buttons">
                <van-button round block type="primary" @click="saveGender">确定</van-button>
            </div>
        </div>
    </van-popup>

    <!-- 生日选择弹窗 -->
    <van-popup v-model="showBirthdayPopup" position="bottom" round>
        <div class="edit-popup">
            <div class="popup-header">
                <div class="popup-title">选择生日</div>
                <van-icon name="cross" @click="showBirthdayPopup = false" />
            </div>
            <div class="birthday-picker">
                <van-datetime-picker v-model="tempBirthday" type="date" title="选择生日" :min-date="minDate"
                    :max-date="maxDate" @confirm="saveBirthday" @cancel="showBirthdayPopup = false" />
            </div>
        </div>
    </van-popup>

    <!-- 个人简介编辑弹窗 -->
    <van-popup v-model="showBioPopup" position="bottom" round>
        <div class="edit-popup">
            <div class="popup-header">
                <div class="popup-title">修改个人简介</div>
                <van-icon name="cross" @click="showBioPopup = false" />
            </div>
            <div class="edit-input">
                <van-field v-model="tempBio" type="textarea" placeholder="介绍一下自己吧" maxlength="100" show-word-limit
                    rows="4" autosize />
            </div>
            <div class="popup-buttons">
                <van-button round block type="primary" @click="saveBio">确定</van-button>
            </div>
        </div>
    </van-popup>

    <!-- 背景图上传弹窗 -->
    <van-popup v-model="showBackgroundPopup" position="bottom" round>
        <div class="upload-popup">
            <div class="popup-title">修改背景图</div>
            <div class="upload-options">
                <div class="upload-option" @click="uploadBackground('camera')">
                    <van-icon name="photograph" size="24" />
                    <span>拍照</span>
                </div>
                <div class="upload-option" @click="uploadBackground('album')">
                    <van-icon name="photo-o" size="24" />
                    <span>从相册选择</span>
                </div>
            </div>
            <div class="cancel-btn" @click="showBackgroundPopup = false">取消</div>
        </div>
    </van-popup>

    <!-- 地区选择弹窗 -->
    <van-popup v-model="showRegionPopup" position="bottom" round>
        <div class="edit-popup">
            <div class="popup-header">
                <div class="popup-title">选择地区</div>
                <van-icon name="cross" @click="showRegionPopup = false" />
            </div>
            <div class="edit-input">
                <van-field v-model="tempRegion" placeholder="请输入地区" />
            </div>
            <div class="popup-buttons">
                <van-button round block type="primary" @click="saveRegion">确定</van-button>
            </div>
        </div>
    </van-popup>

    <!-- 职业选择弹窗 -->
    <van-popup v-model="showOccupationPopup" position="bottom" round>
        <div class="edit-popup">
            <div class="popup-header">
                <div class="popup-title">选择职业</div>
                <van-icon name="cross" @click="showOccupationPopup = false" />
            </div>
            <div class="edit-input">
                <van-field v-model="tempOccupation" placeholder="请输入职业" />
            </div>
            <div class="popup-buttons">
                <van-button round block type="primary" @click="saveOccupation">确定</van-button>
            </div>
        </div>
    </van-popup>

    <!-- 学校选择弹窗 -->
    <van-popup v-model="showSchoolPopup" position="bottom" round>
        <div class="edit-popup">
            <div class="popup-header">
                <div class="popup-title">选择学校</div>
                <van-icon name="cross" @click="showSchoolPopup = false" />
            </div>
            <div class="edit-input">
                <van-field v-model="tempSchool" placeholder="请输入学校" />
            </div>
            <div class="popup-buttons">
                <van-button round block type="primary" @click="saveSchool">确定</van-button>
            </div>
        </div>
    </van-popup>
</div>
</template>

<script>
export default {
    name: 'ProfileEditPage',
    data() {
        return {
            userInfo: {
                avatar: '',
                nickname: '',
                userId: '',
                gender: 0, // 0-保密，1-男，2-女
                birthday: '',
                bio: '',
                backgroundImage: '',
                region: '',
                occupation: '',
                school: ''
            },
            showUploadPopup: false,
            showNicknamePopup: false,
            showGenderPopup: false,
            showBirthdayPopup: false,
            showBioPopup: false,
            showBackgroundPopup: false,
            showRegionPopup: false,
            showOccupationPopup: false,
            showSchoolPopup: false,
            tempNickname: '',
            tempGender: 0,
            tempBirthday: new Date(),
            tempBio: '',
            tempRegion: '',
            tempOccupation: '',
            tempSchool: '',
            minDate: new Date(1900, 0, 1),
            maxDate: new Date()
        }
    },
    computed: {
        genderText() {
            const genderMap = {
                0: '保密',
                1: '男',
                2: '女'
            }
            return genderMap[this.userInfo.gender]
        }
    },
    methods: {
        uploadAvatar() {
            // 模拟上传头像
            this.$toast.loading({
                message: '上传中...',
                forbidClick: true
            })

            setTimeout(() => {
                this.$toast.clear()
                // 随机生成一个新头像
                const randomId = Math.floor(Math.random() * 1000)
                this.userInfo.avatar = `https://i.pravatar.cc/150?img=${randomId}`
                this.showUploadPopup = false
                this.$toast.success('头像上传成功')
            }, 1500)
        },

        uploadBackground() {
            // 模拟上传背景图
            this.$toast.loading({
                message: '上传中...',
                forbidClick: true
            })

            setTimeout(() => {
                this.$toast.clear()
                // 随机生成一个新背景图
                const randomId = Math.floor(Math.random() * 10)
                this.userInfo.backgroundImage = `https://picsum.photos/600/300?random=${randomId}`
                this.showBackgroundPopup = false
                this.$toast.success('背景图上传成功')
            }, 1500)
        },

        saveNickname() {
            if (!this.tempNickname.trim()) {
                this.$toast('昵称不能为空')
                return
            }

            this.userInfo.nickname = this.tempNickname
            this.showNicknamePopup = false
            this.$toast('昵称已更新')
        },

        saveGender() {
            this.userInfo.gender = this.tempGender
            this.showGenderPopup = false
            this.$toast('性别已更新')
        },

        saveBirthday() {
            const year = this.tempBirthday.getFullYear()
            const month = this.tempBirthday.getMonth() + 1
            const day = this.tempBirthday.getDate()

            this.userInfo.birthday = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`
            this.showBirthdayPopup = false
            this.$toast('生日已更新')
        },

        saveBio() {
            this.userInfo.bio = this.tempBio
            this.showBioPopup = false
            this.$toast('个人简介已更新')
        },

        saveRegion() {
            this.userInfo.region = this.tempRegion
            this.showRegionPopup = false
            this.$toast('地区已更新')
        },

        saveOccupation() {
            this.userInfo.occupation = this.tempOccupation
            this.showOccupationPopup = false
            this.$toast('职业已更新')
        },

        saveSchool() {
            this.userInfo.school = this.tempSchool
            this.showSchoolPopup = false
            this.$toast('学校已更新')
        },

        saveProfile() {
            // 保存所有个人资料
            this.$toast.loading({
                message: '保存中...',
                forbidClick: true
            })

            setTimeout(() => {
                // 更新Vuex中的用户信息
                this.$store.commit('SET_USER', {
                    ...this.$store.state.user,
                    ...this.userInfo
                })

                this.$toast.success('保存成功')
                this.$router.go(-1)
            }, 1000)
        }
    },
    created() {
        // 获取当前用户信息
        const user = this.$store.state.user
        if (user) {
            this.userInfo = {
                avatar: user.avatar || 'https://i.pravatar.cc/150?img=1',
                nickname: user.nickname || '纵横',
                userId: user.userId || '4286474693',
                gender: user.gender || 0,
                birthday: user.birthday || '',
                bio: user.bio || '',
                backgroundImage: user.backgroundImage || 'https://picsum.photos/300/400?random=1',
                region: user.region || '',
                occupation: user.occupation || '',
                school: user.school || ''
            }

            // 初始化临时变量
            this.tempNickname = this.userInfo.nickname
            this.tempGender = this.userInfo.gender
            this.tempBio = this.userInfo.bio
            this.tempRegion = this.userInfo.region
            this.tempOccupation = this.userInfo.occupation
            this.tempSchool = this.userInfo.school

            if (this.userInfo.birthday) {
                const [year, month, day] = this.userInfo.birthday.split('-')
                this.tempBirthday = new Date(year, month - 1, day)
            }
        }
    }
}
</script>

<style scoped>
.profile-edit-container {
    min-height: 100vh;
    background-color: #f7f8fa;
    padding-bottom: 90px;
}

.nav-bar {
    background-color: transparent;
}

.nav-bar :deep(.van-nav-bar__title) {
    font-size: 18px;
    font-weight: 500;
}

.nav-bar :deep(.van-nav-bar__left),
.nav-bar :deep(.van-nav-bar__right) {
    font-size: 16px;
    color: #333;
}

.profile-content {
    padding: 0 16px;
}

.profile-form {
    margin: 12px 0 0;
    background-color: #fff;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.mt {
    margin-top: 12px;
}

.form-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    border-bottom: 0.5px solid #f0f0f0;
}

.form-item:last-child {
    border-bottom: none;
}

.form-label {
    font-size: 17px;
    color: #333;
    font-weight: 400;
}

.form-value {
    display: flex;
    align-items: center;
    color: #999;
    font-size: 17px;
}

.arrow-icon {
    margin-left: 8px;
    color: #cccccc;
    font-size: 14px;
}

.avatar-value {
    display: flex;
    align-items: center;
}

.avatar-container {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    overflow: hidden;
    margin-right: 4px;
    border: 0.5px solid #f0f0f0;
}

.avatar-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.bg-preview {
    width: 46px;
    height: 46px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 4px;
    border: 0.5px solid #f0f0f0;
}

.bg-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.value-text {
    max-width: 180px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.bio-text {
    max-width: 180px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.save-button {
    position: fixed;
    bottom: 40px;
    left: 0;
    right: 0;
    padding: 0 52px;
}

.save-button :deep(.van-button) {
    height: 44px;
    line-height: 44px;
    font-size: 16px;
    font-weight: 500;
    border: none;
    box-shadow: 0 2px 8px rgba(254, 73, 109, 0.3);
}

/* 弹窗样式 */
.upload-popup {
    padding: 20px;
}

.popup-title {
    text-align: center;
    font-size: 16px;
    font-weight: 500;
    margin-bottom: 20px;
}

.upload-options {
    display: flex;
    justify-content: space-around;
    padding: 20px 0;
}

.upload-option {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.upload-option span {
    margin-top: 10px;
    font-size: 14px;
}

.cancel-btn {
    margin-top: 20px;
    padding: 15px 0;
    text-align: center;
    font-size: 16px;
    color: #1989fa;
    border-top: 1px solid #ebedf0;
}

.edit-popup {
    padding: 20px;
}

.popup-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.edit-input {
    margin-bottom: 20px;
}

.popup-buttons {
    margin-top: 20px;
}

.gender-options {
    display: flex;
    justify-content: space-around;
    padding: 20px 0;
}

.gender-option {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 15px 30px;
    border-radius: 8px;
    border: 1px solid #ebedf0;
}

.gender-option span {
    margin-top: 10px;
    font-size: 14px;
}

.gender-option.active {
    background-color: #f0f9ff;
    border-color: #1989fa;
}

.birthday-picker {
    height: 280px;
}
</style>